#!/bin/bash
# Generates PKGBUILD `pkgver()` functions for various SCM systems.

usage() {
	cat >&2 <<EOF
usage: pkgver <vcs>
Where <vcs> is one of the following.
 - git / git-tag
 - git-notag
 - svn / subversion
 - hg / mercurial
 - bzr / bazaar
 - fallback / default / date
EOF
}

# Handle errors.
if [[ $# == 0 ]] ; then
	usage
	exit 1
fi

case "$1" in
	-h|--help)
		usage
		exit
		;;
esac

cat <<\EOF
pkgver() {
  cd local_repo
EOF

case "$1" in
	git-tag)
		echo "  git describe --always | sed 's|-|.|g'"
		;;
	git-notag|git)
		echo '  echo $(git rev-list --count HEAD).$(git rev-parse --short HEAD)'
		;;
	svn|subversion)
		echo '  svnversion | tr -d [A-z]'
		;;
	hg|mercurial)
		echo '  echo $(hg identify -n).$(hg identify -i)'
		;;
	bzr|bazaar)
		echo '  bzr revno'
		;;
	date)
		echo '  date +%Y%m%d'
		;;
esac

echo '}'
